package com.example.chessapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class MyList3Activity2 extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{
    MyAdapter adapter;
    public static final String  TAG = "Map";
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list32);

        listView =findViewById(R.id.mylist3);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        ProgressBar progressBar = findViewById(R.id.progressBar);

        Handler hander = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what == 7){
                    ArrayList<HashMap<String,String>> listItems = (ArrayList<HashMap<String,String>>)msg.obj;//HashMap换成RateItem
                    adapter = new MyAdapter(MyList3Activity2.this,R.layout.list_item,listItems);//换成RateAdapter
                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                }
                super.handleMessage(msg);
            }
        };
        Mytask_map task = new Mytask_map();//换用RateItem
        task.setHandler(hander);

        Thread t = new Thread(task);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object itemAtPosition = listView.getItemAtPosition(position);
        HashMap<String,String> map = (HashMap<String,String>) itemAtPosition;
        String titleStr = map.get("ItemTitle");
        String detailStr = map.get("ItemDetail");
        Log.i(TAG,"onItemClick:titleStr:"+titleStr);
        Log.i(TAG,"onItemClick:detailStr:"+detailStr);
//      adapter.remove(itemAtPosition);
//      listView.setEmptyView(findViewById(R.id.nodata32));
        Intent intent = new Intent(MyList3Activity2.this,Rate2Activity.class);
        intent.putExtra("titleStr", titleStr);
        intent.putExtra("detailStr",detailStr);
        startActivity(intent);
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"长按事件");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Object itemAtPosition = listView.getItemAtPosition(position);
                        adapter.remove(itemAtPosition);
                    }
        }).setNegativeButton("否",null);
        listView.setEmptyView(findViewById(R.id.nodata32));
        builder.create().show();
        return true;//长按屏蔽单击
    }
}